package cn.xiaoyu.su.controller;


import cn.xiaoyu.su.controller.request.LinkGroupAddRequest;
import cn.xiaoyu.su.controller.request.LinkGroupUpdateRequest;
import cn.xiaoyu.su.enums.BizCodeEnum;
import cn.xiaoyu.su.service.LinkGroupService;
import cn.xiaoyu.su.util.JsonData;
import cn.xiaoyu.su.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ⼆当家⼩D
 * @since 2022-12-03
 */
@RestController
@RequestMapping("/api/link_group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;
    /**
     * 创建分组
     * @param linkGroupAddRequest
     * @return
     */
    @PostMapping("add")
    public JsonData add(@RequestBody LinkGroupAddRequest linkGroupAddRequest){

        int rows = linkGroupService.add(linkGroupAddRequest);

        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("del/{group_id}")
    public JsonData del(@PathVariable("group_id")long groupId) {

        int rows = linkGroupService.del(groupId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /**
     * 查询分组详情
     * @param groupId
     * @return
     */
    @GetMapping("detail/{group_id}")
    public JsonData detail(@PathVariable("group_id")long groupId) {

        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);

        return linkGroupVO == null ? JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST) : JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 查询用户全部分组(不需要分页）
     * @return
     */
    @GetMapping("list")
    public JsonData list() {
        List<LinkGroupVO> linkGroupVOS = linkGroupService.list();

        return linkGroupVOS==null ? JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST) : JsonData.buildSuccess(linkGroupVOS);

    }

    /**
     * 更新分组标题
     * @param updateRequest
     * @return
     */
    @PutMapping("update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest updateRequest) {

        int rows = linkGroupService.update(updateRequest);

        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }
}

